﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Portal.Core;
using Portal.Systems.Model;
using Portal.Web.Utility;
using Portal.Web.Model;
using Portal.Utility;
using Portal.Workflow.Library;

namespace Portal.Systems.Render
{
    public class WorkflowDefRoleConfigRenderBll : WebPartTemplate
    {
        public override string Id
        {
            get
            {
                return "WorkflowDefRoleConfigRenderBll";
            }
        }

        public override string Title
        {
            get
            {
                return "Thiết lập bảng map vai trò với quy trình";
            }
        }



        static void CheckPermission(string SiteId)
        {
            try
            {
                string UserId = WebSessionUtility.GetCurrentLoginUser().UserId;
                SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CheckPermission(SiteId, SystemPermission.StaticId, SystemPermission.funcManage, SystemPermission.funcManage, UserId, true);
            }
            catch (Exception ex)
            {
                string ReturnData = "<div class=CssAccessDenied>" + ex.Message.ToString() + " <span class=CssBack><a href=\"javascript:history.back(-1);\">Quay lại</a></span></div>\r\n";
                throw new Exception(ReturnData);
            }
        }
        public static AjaxOut Draw(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                CheckPermission(SiteId);
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);

                string WorkflowDefId = (string)WebEnvironments.Request(SiteId, "WorkflowDefId");
                if (string.IsNullOrEmpty(WorkflowDefId)) throw new Exception("Tham số quy trình không hợp lệ!");
                WorkflowDefCls OWorkflowDefCls = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CreateWorkflowDefModel(SiteId, WorkflowDefId);
                if (OWorkflowDefCls == null)
                {
                    throw new Exception("Tham số quy trình không hợp lệ! Quy trình bị xóa hoặc không tìm thấy!");
                }
                
                string ButtonIconRoot = WebConfig.GetButtonIconRoot(OSiteParam);
                string Html =
                   "<script language=javascript>\r\n" +
                   "    function SaveMapRole(WorkflowDefId)\r\n" +
                   "    {\r\n" +
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        Max = parseInt(document.getElementById('txtMax').value);\r\n"+
                   "        SaveData='';\r\n"+
                   "        for(var iIndex=0;iIndex<Max;iIndex++)\r\n"+
                   "        {\r\n"+
                   "            WorkflowDefMapRoleId=document.getElementById('txtId'+iIndex).value;\r\n"+
                   "            RoleId=document.getElementById('drpSelectRole'+WorkflowDefMapRoleId).value;\r\n"+
                   "            WorkflowRoleId=document.getElementById('txtWorkflowRole'+WorkflowDefMapRoleId).value;\r\n" +
                   "            SaveData=SaveData+WorkflowDefMapRoleId+'~'+WorkflowRoleId+'~'+RoleId+';';\r\n" +
                   "        }\r\n"+
                   "        AjaxOut = Portal.Systems.Render.WorkflowDefRoleConfigRenderBll.ServerSideSaveMapRole(SiteId,WorkflowDefId,SaveData).value;\r\n" +
                   "        if(AjaxOut.Error)\r\n" +
                   "        {\r\n" +
                   "            alert(AjaxOut.InfoMessage);\r\n" +
                   "            return;\r\n" +
                   "        }\r\n" +
                   "        alert(AjaxOut.InfoMessage);\r\n"+
                   "    }\r\n" +


                   "</script>\r\n" +
                   "<div class=FormFunction id=divListForm>\r\n" +
                   " <div class=DatabaseFunction>\r\n" +
                   "        <ul>\r\n" +
                   "            <li onclick=\"javascript:SaveMapRole('"+WorkflowDefId+"');\"><img class=image src=\"" + ButtonIconRoot + "/Save.png\"><div>Ghi lại thay đổi</div></li>\r\n" +
                   "            <li onclick=\"javascript:window.open('WorkflowMan.aspx','_self');\"><img class=image src=\"" + ButtonIconRoot + "/back.png\"><div>Quay lại</div></li>\r\n" +
                   "        </ul>\r\n" +
                   "  </div>\r\n" +
                   "  <div id=\"divListWorkflowDef\" class=tabcontent>\r\n" +
                         ServerSideReading(SiteId, WorkflowDefId).HtmlContent +
                   "  </div>\r\n" +
                   "</div>\r\n";

                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = "<div class=CssAccessDenied>" + ex.Message.ToString() + "</div>\r\n";
            }

            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideSaveMapRole(
            string SiteId, 
            string WorkflowDefId, 
            string SaveData)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string[] Items = SaveData.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                WorkflowDefMapRoleCls[] WorkflowDefMapRoles = new WorkflowDefMapRoleCls[Items.Length];
                for (int iIndex = 0; iIndex < Items.Length; iIndex++)
                {
                    string[] SubItems=Items[iIndex].Split(new char[]{'~'},StringSplitOptions.RemoveEmptyEntries);
                    WorkflowDefMapRoles[iIndex] = new WorkflowDefMapRoleCls();
                    WorkflowDefMapRoles[iIndex].WorkflowDefMapRoleId=SubItems[0];
                    WorkflowDefMapRoles[iIndex].WorkflowRoleId = SubItems[1];
                    if (SubItems.Length > 2)
                    {
                        WorkflowDefMapRoles[iIndex].MapGroupRoleId = SubItems[2];
                    }
                    else
                    {
                        WorkflowDefMapRoles[iIndex].MapGroupRoleId = null;
                    }
                }
                SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).SaveWorkflowDefMapRoles(SiteId,WorkflowDefId, WorkflowDefMapRoles);
                OAjaxOut.InfoMessage = "Cập nhật thành công bảng map vị trí công việc!";
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }
        

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideReading(string SiteId,string WorkflowDefId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WorkflowDefCls OWorkflowDefCls = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CreateWorkflowDefModel(SiteId, WorkflowDefId);
                WorkFlowApp OWorkFlowApp = new WorkFlowApp(OWorkflowDefCls.XmlData);
                //WorkflowDefCls[] WorkflowDefs = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).ReadingWorkflowDef(SiteId);
                string Subject = "Thiết lập vị trí công việc cho quy trình - "+OWorkflowDefCls.WorkFlowName;
                RoleCls[] WorkflowRoles = new RoleCls[OWorkFlowApp.ArrayRoles.Count];
                for (int iIndex = 0; iIndex < OWorkFlowApp.ArrayRoles.Count; iIndex++)
                {
                    Portal.Workflow.Model.RoleCls
                        ORole = (Portal.Workflow.Model.RoleCls)OWorkFlowApp.ArrayRoles[iIndex];
                    WorkflowRoles[iIndex] = new RoleCls();
                    WorkflowRoles[iIndex].RoleId=ORole.RoleId;
                    WorkflowRoles[iIndex].RoleName = ORole.RoleName;
                }
                GroupRoleCls[] GroupRoles=SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).ReadingGroupRoles(SiteId,null);
                WorkflowDefMapRoleCls[] WorkflowDefMapRoles = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).ReadingWorkflowDefMapRoles(SiteId, WorkflowDefId, WorkflowRoles);
                string Html =
                   "    <input type=hidden id=txtMax value=\"" + WorkflowDefMapRoles.Length + "\">\r\n" +
                   "        <h3 style=\"text-align:center;\">" + Subject + "</h3>\r\n" +
                   "            <table>\r\n" +
                   "                <tr>\r\n" +
                   "                    <th style=\"text-align:right;margin-right:4px;\">Nhóm đối tượng xử lý trên quy trình</th>\r\n" +
                   "                    <th>Ứng với vị trí công việc</th>\r\n" +
                   "                </tr>\r\n";
                for (int iIndex = 0; iIndex < WorkflowDefMapRoles.Length; iIndex++)
                {
                    //string ConfigButton = "<input type=button class=button value=\"Thiết lập\" onclick=\"javascript:window.open('WorkflowManRoleConfig.aspx?WorkflowId=" + WorkflowDefs[iIndex].WorkflowDefId + "','_self');\">";
                    string SelectGroupRoleText = 
                        "<select type=drplist class=drplist id=\"drpSelectRole" + WorkflowDefMapRoles[iIndex].WorkflowDefMapRoleId + "\">\r\n"+
                        "   <option value=\"\">Chưa xác định quan hệ</option>\r\n";
                    for (int iIndexGroupRole = 0; iIndexGroupRole < GroupRoles.Length; iIndexGroupRole++)
                    {
                        if (WorkflowDefMapRoles[iIndex].MapGroupRoleId == null)
                        {
                            WorkflowDefMapRoles[iIndex].MapGroupRoleId = "";
                        }
                        if (GroupRoles[iIndexGroupRole].GroupRoleId.Equals(WorkflowDefMapRoles[iIndex].MapGroupRoleId))
                        {
                            SelectGroupRoleText += "   <option selected value=\"" + GroupRoles[iIndexGroupRole].GroupRoleId + "\">" + GroupRoles[iIndexGroupRole].GroupRoleName + "</option>\r\n";
                        }
                        else
                        {
                            SelectGroupRoleText += "   <option value=\"" + GroupRoles[iIndexGroupRole].GroupRoleId + "\">" + GroupRoles[iIndexGroupRole].GroupRoleName + "</option>\r\n";
                        }
                    }
                    SelectGroupRoleText += "</select>";
                    Html +=
                       "    <tr>\r\n" +
                       "        <td style=\"text-align:right;margin-right:4px;font-weight:bold\"><input type=hidden id=\"txtId" + iIndex + "\" value=\"" + WorkflowDefMapRoles[iIndex].WorkflowDefMapRoleId + "\"><input id=\"txtWorkflowRole" + WorkflowDefMapRoles[iIndex].WorkflowDefMapRoleId + "\" type=hidden value=\"" + WorkflowDefMapRoles[iIndex].WorkflowRoleId + "\">" + WorkflowDefMapRoles[iIndex].WorkflowRoleName + "</td>\r\n" +
                       "        <td>" + SelectGroupRoleText + "</td>\r\n" +
                       "   </tr>\r\n";
                }
                Html +=
                   "    </table>\r\n";

                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }
    }
}
